//
//  NSDate+Extension.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/16.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit
extension NSDate {

     class func sinaDate(createAt: String) -> NSDate {
//        {
//            
//            guard let createAt = status?.created_at else {
//                
//                return nil
//                
//            }
            let dateformater = NSDateFormatter()
            dateformater.locale = NSLocale(localeIdentifier: "en_US")
            dateformater.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
            let createATdate = dateformater.dateFromString(createAt)
        return createATdate!
    }
    var sinaDateString: String? {
        let dateFormatter = NSDateFormatter()
        dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
            if isThisYear(self)
            {
                let calendar = NSCalendar.currentCalendar()
                if calendar.isDateInToday(self)
                {
                    let timeInterval = abs(self.timeIntervalSinceNow)
                    
                    if timeInterval < 60
                    {
                        
                        return "刚刚"
                        
                    }else if timeInterval < 3600 {
                        let result = timeInterval / 60
                        return "\(Int(result))分钟前"
                    }else {
                        
                        let result = timeInterval / 3600
                        return "\(Int(result))小时前"
                        
                    }
                    
                }else if calendar.isDateInYesterday(self){
                    
                    dateFormatter.dateFormat = "昨天 HH:mm"
                    
                }else{
                    
                    dateFormatter.dateFormat = "MM-dd HH:mm"
                    
                }
                
                
            }else{
                
                dateFormatter.dateFormat = "yyyy-MM-dd HH:MM"
                
            }
            return dateFormatter.stringFromDate(self)

        }
//    }
    private func isThisYear (createAtDate : NSDate) ->Bool {
        let currentDate = NSDate()
        let dateFormatter = NSDateFormatter()
        dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
        dateFormatter.dateFormat = "yyyy"
        let currentDateYear = dateFormatter.stringFromDate(currentDate)
        let createAtDateYear = dateFormatter.stringFromDate(createAtDate)
        return currentDateYear == createAtDateYear
        
    }
}